package com.jack.leetcode.eachproblem_2020;

import java.util.*;

public class 分割数组为连续子序列 {

    public static void main(String[] args) {
        int[] a = {1,2,3,3,4,5};
        int[] b = {1,2,3,3,4,4,5,5};
        int[] c = {1,2,3,4,4,5};
        System.out.println(isPossible(a));
        System.out.println(isPossible(b));
        System.out.println(isPossible(c));
    }

    static class entity{
        int key;
        int value;

        public entity(int key, int value){
            this.key = key;
            this.value = value;
        }
    }

    public static boolean isPossible(int[] nums) {
        if(nums == null || nums.length < 3){
            return false;
        }
        Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
        for(int i : nums){
            if(!map.containsKey(i)){
                map.put(i, new PriorityQueue<>());
            }
            if(map.containsKey(i - 1)){
                Integer poll = map.get(i - 1).poll();
                if(map.get(i - 1).isEmpty()){
                    map.remove(i - 1);
                }
                map.get(i).offer(poll + 1);
            }else{
                map.get(i).offer(1);
            }
        }
        for (Map.Entry<Integer, PriorityQueue<Integer>> entry : map.entrySet()) {
            if(entry.getValue().peek() < 3){
                return false;
            }
        }
        return true;
    }
}
